package leetcode;

/**
 * 描述:92. 反转链表 II
 *
 * @author Madison You
 * @created 14:50
 */
public class Mid_92_ReverseBetween {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        ListNode node = Mid_2_AddTwoNumbers.createNode(arr);
        new Mid_92_ReverseBetween().reverseBetween(node, 2, 4);
    }

    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dynamic = new ListNode(-1);
        dynamic.next = head;
        ListNode g = dynamic;
        ListNode p = dynamic.next;
        for (int i = 0; i < left - 1; i++) {
            g = g.next;
            p = p.next;
        }
        for (int i = 0; i < right - left; i++) {
            ListNode removed = p.next;
            p.next = p.next.next;
            removed.next = g.next;
            g.next = removed;
        }
        return dynamic.next;
    }

}
